<?php

class Media extends CI_Controller {
	
	 public function __construct(){
		parent::__construct();		
		if(!is_admin_role('admin'))
			redirect(base_url('admin/login'));
		$this->load->model('Media_m', 'media');		
    }
	
	function index() {
		$data = array();
		$tmp = array();
		// $sql = "INSERT INTO media(`file_name`,`desc`,`width`,`height`,`type`,`size`,`path`,`created`,`modify`) VALUES ";
		// foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('uploads/images/full')) as $f){
			// $tmp = getimagesize($f->getPath().'/'.$f->getFilename());
			// $sql .= "('{$f->getFilename()}',";
			// $sql .= "'',";
			// $sql .= "{$tmp[0]},";
			// $sql .= "{$tmp[1]},";
			// $sql .= "'{$tmp['mime']}',";
			// $sql .= "{$f->getSize()},";
			// $sql .= "'{$f->getPath()}/{$f->getFilename()}',";
			// $sql .= "'".date('Y-m-d H:i:s')."',";
			// $sql .= "NULL),";
		// }	//$data['media'][] = $f->getFilename();
		// echo $sql;die;
		$config['per_page'] = 10;
		$config['base_url'] = base_url('admin/media');
		$config['use_page_numbers'] = TRUE;
		$config['num_links'] = 5;
		$config['full_tag_open'] = '<ul>';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a>';
		$config['cur_tag_close'] = '</a></li>';		
		$page = $this->uri->segment(3);
		$offset = ($page * $config['per_page']) - $config['per_page'];
		$page = $page > 1 ? $page - 1 : 0;
		$data['stt']		= $page * $config['per_page'] + 1;
		$filter = array(
			'offset'	=>	$offset
		);		
		$config['total_rows'] = $this->media->get_media_list($filter, true);
		$this->pagination->initialize($config);
		$data['media'] = $this->media->get_media_list($filter);
		// echo '<pre>';print_r($data['media']);die;
		$data['main_content'] = 'admin/media';
      $this->load->view('admin/includes/template', $data);
	}
	
	function delete($id){
		$media	= $this->media->get_media($id);
		if($media){
			$this->media->delete_media($id);
			unlink($media->path);
			$this->session->set_flashdata('message', 'Xóa thành công');
		}else
			$this->session->set_flashdata('error', 'Có lỗi xảy ra');
		redirect('admin/media');
	}
	
	function form($id = false){
		$media	= $this->media->get_media($id);
		$data = array();
		if($media){
			$data['media']	= $media;
		}else
			redirect('admin/media');
		$data['main_content'] = 'admin/media_form';
      $this->load->view('admin/includes/template', $data);
	}
	
	function upload(){
		$data['main_content'] = 'admin/upload_form';
      $this->load->view('admin/includes/template', $data);
	}
	
	function crop(){
		$post = $this->input->post();
		if($post){
			extract($post);			
			if(!isset($id))
				redirect('admin/media');
			$media	= $this->media->get_media($id);
			// print_r($media);die('adawda');
			if(!$media)
				redirect('admin/media');
			$inFile = $_SERVER['DOCUMENT_ROOT'].'/'.$media->path;
			// $outFile = $_SERVER['DOCUMENT_ROOT']."/uploads/images/test-cropped.jpg";
			//
			$image = new Imagick($inFile);
			$image->cropImage($w, $h, $x, $y);
			if($image->writeImage($inFile)){
				$data = array();
				$data['id']	=	$id;
				$data['width']	= $w;
				$data['height']= $h;
				$data['size']	= filesize($inFile);
				$data['modify']= date('Y-m-d H:i:s');
				$this->media->save_media($data);
				$this->session->flashdata('message', 'Crop thành công');
			}else
				$this->session->flashdata('error', 'Có lỗi xảy ra');
		}
		redirect($url);
	}
	
	function rotate(){
		$post = $this->input->post();
		if($post){
			extract($post);			
			if(!isset($id))
				redirect('admin/media');
			$media	= $this->media->get_media($id);
			// print_r($media);die('adawda');
			if(!$media)
				redirect('admin/media');
			$inFile = $_SERVER['DOCUMENT_ROOT'].'/'.$media->path;
			// $outFile = $_SERVER['DOCUMENT_ROOT']."/uploads/images/test-cropped.jpg";
			//
			$image = new Imagick($inFile);
			$image->rotateImage(new ImagickPixel('#fff'), $degree);
			if($image->writeImage($inFile)){
				$data = array();
				$data['id']	=	$id;
				$data['size']	= filesize($inFile);
				$data['modify']= date('Y-m-d H:i:s');
				$this->media->save_media($data);
				$this->session->flashdata('message', 'Chỉnh sửa thành công');
			}else
				$this->session->flashdata('error', 'Có lỗi xảy ra');
		}
		redirect($url);
	}
}